package com.demo.business.goods;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 商品表
 * @create: 2022-02-25 23:30:07
 **/
@Service("goods")
@Slf4j
public class GoodsService{

    @Resource
    GoodsDao goodsDao;

    /**
     * 添加商品表,获取lGoodsId数据
     *
     * @param cdoRequest - lGoodsId:
     *                   - sGoodsName:商品名称
     *                   - sImgUrl:商品图片路径
     *                   - nGoodsRealFee:商品实际金额，0代表自定义金额
     *                   - nGoodsSaleFee:商品售卖金额，0代表自定义金额
     *                   - sSynopsis:商品简介
     *                   - nGoodsType:定单类型：1 会员商品,2、促销商品 
     *                   - lLabelId:商品标签Id
     *                   - lCategoryId:商品类别id
     *                   - sServiceTime:服务时长
     *                   - sCall:是否可以上门：0、可以，1、不可以，2、已上门
     *                   - nSort:排序
     *                   - bRecommend:是否推荐
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     *                   - iState:商品状态：0、下架 ，1、已上架
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return addGoods(CDO cdoRequest) {
        goodsDao.addGoods(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lGoodsId")) {
            cdoResult.set("lGoodsId", cdoRequest.get("lGoodsId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lGoodsId，更新商品表数据
     *
     * @param cdoRequest - lGoodsId:
     *                   - sGoodsName:商品名称
     *                   - sImgUrl:商品图片路径
     *                   - nGoodsRealFee:商品实际金额，0代表自定义金额
     *                   - nGoodsSaleFee:商品售卖金额，0代表自定义金额
     *                   - sSynopsis:商品简介
     *                   - nGoodsType:定单类型：1 会员商品,2、促销商品 
     *                   - lLabelId:商品标签Id
     *                   - lCategoryId:商品类别id
     *                   - sServiceTime:服务时长
     *                   - sCall:是否可以上门：0、可以，1、不可以，2、已上门
     *                   - nSort:排序
     *                   - bRecommend:是否推荐
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     *                   - iState:商品状态：0、下架 ，1、已上架
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return updateGoodsById(CDO cdoRequest) {
        int affectedRows = goodsDao.updateGoodsById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lGoodsId，删除商品表数据
     *
     * @param cdoRequest - lGoodsId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    //@OpenApi
    public Return deleteGoodsById(CDO cdoRequest){
        int affectedRows = goodsDao.deleteGoodsById(cdoRequest.getLong("lGoodsId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除商品表数据
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    //@OpenApi
    public Return deleteGoods(CDO cdoRequest) {
        int affectedRows = goodsDao.deleteGoods(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取商品表数据
     *
     * @param cdoRequest - lGoodsId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return getGoodsById(CDO cdoRequest) {
        CDO cdoMapperResult = goodsDao.getGoodsById(cdoRequest.getLong("lGoodsId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取商品表数据
     *
     * @param cdoRequest - lGoodsId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return getGoods(CDO cdoRequest) {
        CDO cdoMapperResult = goodsDao.getGoods(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取商品表数据列表
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return getGoodsList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = goodsDao.getGoodsList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = goodsDao.getGoodsListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取商品表数据列表
     *
     * @param cdoRequest - lGoodsId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:07
     */
    @OpenApi
    public Return getGoodsListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = goodsDao.getGoodsListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = goodsDao.getGoodsListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
